﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace shopxachtayphap
{
    public partial class UploadProduct : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            using (var shop = new WebxachtayEntities())
            {
                LoadAllCategory(shop);
                LoadAllNsx(shop);
            }
        }

        private void LoadAllNsx(WebxachtayEntities shop)
        {
            var nsx = shop.Manufacturers.ToList();
            drNsx.DataSource = nsx;
            drNsx.DataTextField = "Name";
            drNsx.DataValueField = "Id";
            drNsx.DataBind();

        }

        private void LoadAllCategory(WebxachtayEntities shop)
        {

            var category = shop.Categories.Where(x => x.Status == 1).ToList();
            DrCategory.DataSource = category;
            DrCategory.DataValueField = "ID";
            DrCategory.DataTextField = "Name";
            DrCategory.DataBind();

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            Upload();
        }

        private void Upload()
        {
            string name = txtName.Text;
            string txtprice = txtPrice.Text;
            string txtdiscount = txtDiscount.Text;
            string description = txtDescription.Text;
            double Price = double.Parse(txtprice);
            string category = DrCategory.SelectedValue;
            int manufacturerId = 0;
            using (var shop = new WebxachtayEntities())
            {
                if (string.IsNullOrEmpty(drNsx.SelectedValue))
                {
                    Manufacturer manufacturer = new Manufacturer()
                                                    {
                                                        Category = category,
                                                        Name = drNsx.Text
                                                    };
                    shop.Manufacturers.Add(manufacturer);
                    shop.SaveChanges();
                    manufacturerId = manufacturer.ID;
                }
                else
                    manufacturerId = int.Parse(drNsx.SelectedValue);
                int? discount = string.IsNullOrEmpty(txtdiscount) ? (int?)null : int.Parse(txtdiscount);

                Product a = new Product()
                                {
                                    Name = name,
                                    CategoryId = category,
                                    Discount = discount,
                                    Price = Price,
                                    ManufactureId = manufacturerId,
                                    Description = description,
                                    Status = 1,
                                    ImagePath = "~/MediaUploader/"
                                };
                shop.Products.Add(a);
                shop.SaveChanges();
            }
        }


    }
}